package whf.file.web;

import java.util.Map;

import whf.file.Constants;
import whf.file.entity.File;
import whf.file.entity.Folder;
import whf.file.entity.Type;
import whf.file.service.FolderServiceImp;
import whf.file.service.TypeServiceImp;
import whf.file.util.DiskFileManager;
import whf.file.util.FileManager;
import whf.framework.entity.AbstractEntity;
import whf.framework.util.StringUtils;
import whf.framework.util.Utils;
import whf.framework.util.images.ScaleProperty;
import whf.framework.web.struts.WebData;
import whf.framework.web.struts.WebDispatchAction;
import whf.framework.web.struts.WebForward;

/**
 * @author wanghaifeng
 * @create Nov 26, 2006 9:59:55 PM
 * 
 */
public class FileAction  extends WebDispatchAction {
	private static FileManager fileManager = DiskFileManager.getFileManager();
	
	/* (non-Javadoc)
	 * @see whf.framework.web.struts.WebDispatchAction#create(whf.framework.entity.AbstractEntity, whf.framework.web.struts.WebData)
	 */
	@Override
	public WebForward create(AbstractEntity baseForm, WebData data) throws Exception {
		Folder folder = (Folder)data.getEntityParameter(Folder.class, "folder", 0, 0);
		data.setAttribute("folderId", folder!=null?folder.getId() + "":"0");
		File file = (File)baseForm;
		if(file.getPhycialPathFile() != null && file.getPhycialPathFile().getFileSize() > 0){
			String fileName = file.getPhycialPathFile().getFileName();
			//设置扩展名
			int index = fileName.lastIndexOf(".") + 1;
			String ext = fileName.substring(index);
			if(StringUtils.isEmpty(ext)){
				file.setExt(ext);
			}
			//根据扩展名设置文件类型
			long typeId = data.getManyToOnePropertyId(Type.class, "type", 0, baseForm.getId());
			if(typeId == 0){
				file.setType(TypeServiceImp.getTypeService().findByExt(ext));
			}
			//如果没有名称，设置文件名
			if(StringUtils.isEmpty(file.getName())){
				file.setName(fileName);
				data.putData("name", fileName);
			}
			if(File.isImageFile(file.getPhycialPathFile().getFileName())){
				Map<String, ScaleProperty> props = Utils.newHashMap();
				props.put(Constants.ICON_TYPE_LARGE, Constants.LARGE_SCALE_PROPERTIES);
				props.put(Constants.ICON_TYPE_MIDDLE, Constants.MIDDLE_SCALE_PROPERTIES);
				props.put(Constants.ICON_TYPE_SMALL, Constants.SMALL_SCALE_PROPERTIES);
				fileName = (folder != null?folder.getPath():"") + file.getPhycialPathFile().getFileName();
				fileName = fileManager.saveImage(file.getPhycialPathFile().getInputStream(), fileName, props);
				file.setPhycialPath(fileName);
				file.setSize(file.getPhycialPathFile().getFileSize());
			} else {
				fileName = (folder != null?folder.getPath():"") + file.getPhycialPathFile().getFileName();
				fileName = fileManager.save(file.getPhycialPathFile().getInputStream(), fileName);
				file.setPhycialPath(fileName);
				file.setSize(file.getPhycialPathFile().getFileSize());
			}
			return super.create(baseForm, data);
		} else {
			data.setAttribute("message", "没有选择文件！");
			return this.prepareCreate(baseForm, data);
		}
	}

	/* (non-Javadoc)
	 * @see whf.framework.web.struts.WebDispatchAction#prepareCreate(whf.framework.entity.AbstractEntity, whf.framework.web.struts.WebData)
	 */
	@Override
	public WebForward prepareCreate(AbstractEntity baseForm, WebData data) throws Exception {
		long folderId = data.getLongParameter("folderId", 0);
		if(folderId > 0){
			Folder folder = FolderServiceImp.getFolderService().findByPrimaryKey(folderId);
			((File)baseForm).setFolder(folder);
		}
		return super.prepareCreate(baseForm, data);
	}

	@Override
	public WebForward delete(AbstractEntity webEntity, WebData data) throws Exception {
		WebForward res = super.delete(webEntity, data);
		Folder folder = ((File)webEntity).getFolder();
		if(folder != null) {
			data.setAttribute("folderId", folder.getId() + "");
		}
		return res;
	}

} 